package com.SmartClassroom.system.controller;

import com.SmartClassroom.system.annotation.AuthCheck;
import com.SmartClassroom.system.common.BaseResponse;
import com.SmartClassroom.system.common.DeleteRequest;
import com.SmartClassroom.system.common.ErrorCode;
import com.SmartClassroom.system.common.ResultUtils;
import com.SmartClassroom.system.constant.UserConstant;
import com.SmartClassroom.system.exception.BusinessException;
import com.SmartClassroom.system.exception.ThrowUtils;
import com.SmartClassroom.system.model.domain.Notification;
import com.SmartClassroom.system.model.dto.notification.NotificationAddRequest;
import com.SmartClassroom.system.model.dto.notification.NotificationQueryRequest;
import com.SmartClassroom.system.model.dto.notification.NotificationUpdateRequest;
import com.SmartClassroom.system.service.NotificationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 通知接口
 *
 * @author baijiayu
 */
@RestController
@RequestMapping("/notification")
@Slf4j
@Api(tags = {"通知接口"})
public class NotificationController {

    @Resource
    private NotificationService notificationService;


    // region 增删改查

    /**
     * 创建通知
     *
     * @param notificationAddRequest
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "创建通知（老师和管理员）")
    public BaseResponse<Integer> addNotification(@RequestBody NotificationAddRequest notificationAddRequest) {
        if (notificationAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Notification notification = new Notification();
        BeanUtils.copyProperties(notificationAddRequest, notification);
        notification.setTimestamp(new Date());
        boolean result = notificationService.save(notification);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(notification.getNotificationId());
    }

    /**
     * 删除通知
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除通知")
    public BaseResponse<Boolean> deleteNotification(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = notificationService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }

    /**
     * 更新通知
     *
     * @param notificationUpdateRequest
     * @return BaseResponse<Boolean>
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "更新通知")
    public BaseResponse<Boolean> updateNotification(@RequestBody NotificationUpdateRequest notificationUpdateRequest) {
        if (notificationUpdateRequest == null || notificationUpdateRequest.getNotificationId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Notification notification = new Notification();
        BeanUtils.copyProperties(notificationUpdateRequest, notification);
        boolean result = notificationService.updateById(notification);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * 获取全部通知
     *
     * @param notificationQueryRequest
     * @return
     */
    @PostMapping("/get/all")
    @ApiOperation(value = "获取全部通知")
    public BaseResponse<Page<Notification>> getNotificationAll(@RequestBody NotificationQueryRequest notificationQueryRequest) {
        long current = notificationQueryRequest.getCurrent() == 0 ? 1 : notificationQueryRequest.getCurrent();
        long size = notificationQueryRequest.getPageSize() == 0 ? 10 : notificationQueryRequest.getPageSize();
        Page<Notification> pageParam = new Page<>(current, size);
        Page<Notification> notificationPage = notificationService.getQueryWrapper(pageParam, notificationQueryRequest);

        return ResultUtils.success(notificationPage);
    }
}
